<?php

class CampaignController extends Controller{
	public $menuActive = "campaigns";


	private function loadModel($id){
		$model= Campaign::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	public function actionIndex(){
		$model= new Campaign('search');
		$model->unsetAttributes();  // clear any default values
		$model->created_by = Yii::app()->user->getId();
		if(isset($_GET['Campaign']))
			$model->attributes = $_GET['Campaign'];

		$this->render('index', array(
			'model' => $model
		));
	}

	public function actionCreate(){
		$step = 0;
		if(!empty($_GET['step']) && is_numeric($_GET['step']) && in_array($_GET['step'], range(0,5))){
			$step = $_GET['step'];
		}

		$this->forward('wizardCampaignStep'.$step);
	}

	public function actionWizardCampaignStep0(){
		$this->render('wizard_campaign/step_intro');
	}

	public function actionWizardCampaignStep1(){
		// register script to change campaign type
		$this->ui->addScript(null, ClientScript::ChangeCampaignType());

		$model = new CampaignStepOneForm();
		$this->ajaxValidate($model);
		if(Yii::app()->request->isPostRequest && !empty($_POST['CampaignStepOneForm'])){
			$model->setAttributes($_POST['CampaignStepOneForm'], false);
			if($model->validate()){
				$campaign = new Campaign();
				$campaign->setAttributes($model->getAttributes(), false);
				$result = $campaign->updateCampaignStepOne();
				if($result === true) $this->redirect(Yii::app()->createUrl('campaigns/campaign/create', array('step' =>2,'id' => $campaign->id)));
			}else{
				if($model->hasErrors('campaignType')){
					Yii::app()->user->setFlash('error', $model->getError('campaignType'));
				}
			}
		}
		$this->render('wizard_campaign/step_one', array(
			'step' => 1,
			'model' => $model
		));
	}

	public function actionWizardCampaignStep2(){
		$id = (empty($_GET['id']))?'':$_GET['id'];
		$model = Campaign::model()->findByPk($id);
		if(!is_object($model))
			$this->redirect(Yii::app()->createUrl('campaigns/campaign/create'));
		// list groups of current user
		// Load UI autosuggest
		$this->ui->loadUI(array('autosuggest'));
		$this->ui->addScript(null,ClientScript::autosuggestContactsCampaign(Yii::app()->createUrl('contacts/search/contacts'),'contacts'));

		$groups = Group::model()->findAllByAttributes(array('created_by' => Yii::app()->user->getId()));
		$contactsInGroup = array();
		foreach($groups as $g){
			foreach($g->groupContacts as $c){
				$contactsInGroup[] = $c->contact_id;
			}
		}
		$contactsInGroup = array_values(array_unique($contactsInGroup));
		// get contacts not in group
		$condContactsNotGroup = new CDbCriteria();
		$condContactsNotGroup->addNotInCondition('id', $contactsInGroup);
		$contactsNotGroup = Contact::model()->findAll($condContactsNotGroup);

		$formModel = new CampaignStepTwoForm();
		if(Yii::app()->request->isPostRequest){
			if(!empty($_POST['CampaignStepTwoForm'])){
				$formModel->setAttributes($_POST['CampaignStepTwoForm'], false);
			}
			if(!empty($_POST['contacts'])){
				$formModel->contacts = explode(',', $_POST['contacts']);
			}

			dd($formModel);

			if($formModel->validate()){
				$result = $formModel->sync($model);
			}
		}
		$this->render('wizard_campaign/step_two', array(
			'step' => 2,
			'model' => $model,
			'formModel' => $formModel,
			'contactsNotGroup' => $contactsNotGroup
		));
	}

	public function actionWizardCampaignStep3(){
		$this->render('wizard_campaign/step_three', array(
			'step' => 3
		));
	}

	public function actionWizardCampaignStep4(){
		$this->render('wizard_campaign/step_four', array(
			'step' => 4
		));
	}

	public function actionWizardCampaignStep5(){
		$this->render('wizard_campaign/step_five', array(
			'step' => 5
		));
	}
}